/*
 * Copyright ITEdge s.r.o 2012
 */

if (typeof Function.prototype.bind !== 'function') {
	Function.prototype.bind = function(scope) {
	  var func = this;
	  
	  return function() {
	    return func.apply(scope, arguments);
	  };
	};     
}

if (typeof Object.create !== 'function') {
    Object.create = function (o) {
        function F() {}
        F.prototype = o;
        return new F();
    };
}

if (typeof String.prototype.template !== 'function') {
	String.prototype.template = function(variable) {
	    var result = this.toString().replace(new RegExp("{var}","g"),variable);
	    return result;
	}; 
}

function addPutParameterToForm(formName) {       	 
	 var form = dojo.byId(formName);
	 dojo.create("input", { type: "hidden", name: "_method", value: "PUT" }, form);
}

function addDeleteParameterToForm(formName) {       	 
	 var form = dojo.byId(formName);
	 dojo.create("input", { type: "hidden", name: "_method", value: "DELETE" }, form);
}
/*
var TableModel = function() {
	this.data = new Array();
};

TableModel.prototype = {
	parseLine: function(item) {
	    return item;
	},
	parseTableData: function(tableData) {
	    this.data = new Array();
	    this.data = dojo.map(tableData, function(item) {
	    	return this.parseLine(item);
	    }.bind(this)); 
	}
};

var TableView = function(domNode, id, headersInfo) {
	this.domNode = domNode;
	this.tableId = id;
	this.headersInfo = headersInfo;
};

TableView.prototype = {
	renderHeaders: function(headersInfo) {
		var tHead = dojo.create("thead");
		var trHeadRow = dojo.create("tr", null, tHead);
		dojo.forEach(headersInfo, function(headerInfo) {
			dojo.create("th", { innerHTML: headerInfo.label }, trHeadRow);
		});
		return tHead;
	},
	renderDataRow: function(dataRow) {
		var trRow = dojo.create("tr");
		dojo.forEach(this.headersInfo, function(headerInfo) {
			var dataToDisplay = dataRow[headerInfo.key];
			if (typeof headerInfo.transform == 'function') {
				dataToDisplay = headerInfo.transform(dataToDisplay);
			}
			// IE workaround
			if (dataToDisplay === null) {
				dataToDisplay = '';
			}
			dojo.create("td", { innerHTML: dataToDisplay }, trRow);
		});
		return trRow;
	},
	renderTable: function(tableModel) {
		this.clearTable();
		var table = dojo.create("table", {id: this.tableId});
		dojo.place(this.renderHeaders(this.headersInfo),table);
		dojo.forEach(tableModel.data, function(dataRow) {
			dojo.place(this.renderDataRow(dataRow),table);
		}.bind(this));
		dojo.place(table,this.domNode);
	},
	clearTable: function() {
		this.domNode.innerHTML = "";
	}
};

var JsonSearchController = function(searchBoxId,serviceUrl,tableModel,tableView,timeOut,maxResults) {
	this.searchBox = dojo.byId(searchBoxId);
	this.serviceUrl = serviceUrl;
	this.tableModel = tableModel;
	this.tableView = tableView;
	this.maxResults = maxResults;
	this.timeOut = timeOut;
	this.init();
};         

JsonSearchController.prototype = {
    init: function() {  	
    	dojo.connect(this.searchBox, "onkeyup", function(evt){
    		this.search(this.searchBox.value.toLowerCase(),this.maxResults);
    	}.bind(this));
    },
    search: function(searchValue, maxResults) { 		
    	dojo.xhrPost({
	        url: this.serviceUrl,
	        headers: { "Accept": "application/json" },
	        handleAs: "json",
	        timeout: this.timeOut,
	        content: {
	            searchName: searchValue,
	            maxResults: maxResults
	        },
	        load: function(jsonData) {
	        	this.tableModel.parseTableData(jsonData);
	            this.tableView.renderTable(tableModel);            
	        }.bind(this),
	        error: function() {
	        	this.searchBox.innerHTML = "Fault";
	        }.bind(this)
        });
    }
};*/